home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13664 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.logicon.com!newsmaster@klee
  2. From: kkolda@logicon.com (Kenneth D. Kolda)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need help with an array of pointers
  5. Date: 26 Mar 1996 17:57:30 GMT
  6. Organization: Logicon Operating Systems
  7. Message-ID: <4j9b6a$num@piper.logicon.com>
  8. References: <3157FBAF.32A3@village.ios.com>
  9. NNTP-Posting-Host: 137.51.122.161
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.2
  12.  
  13. In article <3157FBAF.32A3@village.ios.com>, kboruff@village.ios.com 
  14. says...
  15. >
  16. >Suppose I have an array of pointers:
  17. >
  18. >char *WordPtr[] = {"Jack", "Joe", "Bob"};
  19. >
  20. >How do I print the address value of each pointer element (i.e., the 
  21. >address that each pointer points to)? The only addresses I seem to be 
  22. >able to print are the addresses of the array elements themselves.
  23. >
  24. >I would appreciate any help on this subject. An array of pointers is a 
  25. >confusing topic to me and I could use all the help I can get.
  26. >
  27. >Keith Boruff
  28. >Long Island, NY
  29.  
  30. Let's make a sample setup in memory:
  31.  
  32. Variable Name     Address(es)       Value
  33. -------------     -----------       -----
  34.  
  35. WordPtr[0]           0x10            0x100
  36. WordPtr[1]           0x11            0x200
  37. WordPtr[2]           0x12            0x300
  38.                    0x100-0x104       'J', 'a', 'c', 'k', '\0'
  39.            0x200-0x203       'J', 'o', 'e', '\0'
  40.            0x300-0x303       'B', 'o', 'b', '\0'
  41.  
  42.  
  43. As you can see, WordPtr[0] points to the name "Jack", WordPtr[1] points 
  44. to "Joe" and WordPtr[2] points to "Bob".
  45.  
  46. Now consider each of the following values:
  47.  
  48. 1)  (int) WordPtr == (int) &WordPtr[0] = 0x10
  49.  
  50.     This returns the address of the array, which is also the
  51.     address of the 0th element of the array (which is why
  52.     we can use WordPtr[i] and *(WordPtr + i) interchangably).
  53.  
  54. 2)  (int) &WordPtr[1] = 0x11
  55.  
  56.     The address of the 1st element of the array WordPtr.  This is 
  57.     what you're getting that you do *not* want.
  58.  
  59. 3)  (int) WordPtr[1] = (int) &WordPtr[1][0] = 0x200
  60.  
  61.     This is the value of WordPtr[1] interpreted as an integer, i.e. 
  62.     what is the value of the pointer in WordPtr[1]??  This is what 
  63.     you're looking for.  The second interpretation is:  What is the 
  64.     address of the 0th element of the string pointed to be 
  65.     WordPtr[1]??  These are equivalent statements.
  66.  
  67. Hope that helps!
  68.  
  69. Ken Kolda
  70.  
  71.